package com.golxen.framework.log;


import com.golxen.framework.manager.TempMgr;
import com.golxen.framework.user.log.entity.LogLogin;
import com.golxen.utils.TimeUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * log保存类
 */
public class LogMgr extends TempMgr {

    static LogMgr instance;

//    private static List<Logxxx> logxxx;
    private static List<LogLogin> logLoginList;
    public boolean init() {

//        logxxx = new ArrayList<>();
        logLoginList = new ArrayList<>();
        instance = this;
        return true;
    }

    /**
     * 定时保存
     */
    public boolean save() {
//        saveLogxxx();
        saveLogLogin();
        return true;
    }

    public boolean stop() {
        return save();
    }

    public static void shutdown() {
        instance.stop();
    }


//    public static void addLogxxx(XXX xxx) {
//        Logxxx log = new Logxxx(XXX xxx);
//        logxxxList.add(log);
//    }
//
//
    public static void addLogLogin(long userId, int type) {
        LogLogin logLogin = new LogLogin();
        logLogin.setUserId(userId);
        logLogin.setType(type);
        logLogin.setTime(TimeUtils.getSysteCurTime());
        synchronized (logLoginList) {
            logLoginList.add(logLogin);
        }
    }
//    private static void saveLogxxx() {
//        if (CollectionUtils.isEmpty(logxxx)) {
//            return;
//        }
//        List<Logxxx> list;
//        synchronized (logxxx) {
//            list = new ArrayList<>(logxxx);
//            logxxxList.clear();
//        }
//        LogBussiness.addLogxxx(list);
//    }

    private static void saveLogLogin() {
        if (!CollectionUtils.isEmpty(logLoginList)) {
            List<LogLogin> list;
            synchronized (logLoginList) {
                list = new ArrayList<>(logLoginList);
                logLoginList.clear();
            }
            LogBussiness.addLogLoginList(list);
        }
    }

}
